<?php
// TODO: Add intval() and other input-sanity-checks here and in the other processors. We don't expect the admins to hack us, but still...
// TODO: Go through the OTHER modules and make sure we return results there that prevent them from having to double-query us.
// TODO: Add ERROR: return results here and elsewhere on all errors
// TODO: Here and on the other screens, after adding an item, clear the form fields
// TODO: Dupe detection, here and elsewhere
// TODO: Arte you sure question on delete
	function options_add() {
		global $kiwee;
		
		$size = intval($_POST['size']);
		$maxlen = intval($_POST['maxlen']);
		$rows = intval($_POST['rows']);
		$cols = intval($_POST['cols']);
		$list = intval($_POST['list']);
		$default = intval($_POST['default']);
		$renderas = addslashes(trim($_POST['renderas']));
		$name = addslashes(trim($_POST['name']));
		$htmlclass = addslashes(trim($_POST['htmlclass']));

		if (strlen($name) < 1 || strlen($renderas) < 1)
			die('ERROR: Please supply a name');
		
		$qry = 'insert into ' . $kiwee->config['fpx'] . 'options (item_id, lists_id, name, renderas, htmlclass, size, maxlen, rows, cols, deflt) values (0, ' .
				$list . ',"' . $name . '","' . $renderas . '","' . $htmlclass . '",' . $size . ',' . $maxlen . ',' . $rows . ',' . $cols . ',' . $default . ')';
		mysql_query($qry);
		options_gridload();
	}		

	function options_listentries() {
		global $kiwee;

		$list = intval($_POST['list']);
		
		$qry = 'select * from ' . $kiwee->config['fpx'] . 'lists_options where lists_id=' . $list;
		$rows = db_getrows($qry);
		if ($rows == NULL)
			die('ERROR: Empty list selected - add some entries to it');

		$lines = array();
		foreach ($rows as $row) {
			$lines[] = '<option value="' . $row['id'] . '">' . $row['name'] . '</option>' . "\n";
		}
		echo implode("\n", $lines);
	}		

	function options_delete() {
		global $kiwee;
		
		$id = intval($_POST['id']);
		
		$qry = 'select ' . $kiwee->config['fpx'] . 'items.name from ' . $kiwee->config['fpx'] . 'items_options left join ' .
		  		$kiwee->config['fpx'] . 'items on ' . $kiwee->config['fpx'] . 'items.id=' . $kiwee->config['fpx'] . 'items_options.item_id ' .
		  		'where ' . $kiwee->config['fpx'] . 'items_options.options_id=' . $id;
		
		$rows = db_getrows($qry);
		if (sizeof($rows) < 1) {
			$qry = 'delete from ' . $kiwee->config['fpx'] . 'options where id=' . $id;
			mysql_query($qry);
			options_gridload();
		} else {
			$lines = array();
			$lines[] = 'ERROR: Option is in use by the following items: ';
			foreach ($rows as $row) {
				$lines[] = $row['name'];
			}
			echo implode("\n", $lines);
		}
	}		

	function options_gridload() {
		global $kiwee, $_lang;
		
		$sql = 'SELECT ' . $kiwee->config['fpx'] . 'options.*,' . $kiwee->config['fpx'] . 'lists.description as listname FROM ' . $kiwee->config['fpx'] .
		  		'options left join ' . $kiwee->config['fpx'] . 'lists on ' . $kiwee->config['fpx'] . 'lists.id=' . $kiwee->config['fpx'] . 'options.lists_id';
		$ds = mysql_query($sql) or die ('ERROR: ' . mysql_error());
		$grd = new DataGrid('',$ds, $number_of_results);
		$grd->noRecordMsg = $_lang['no_records_found'];
		$grd->cssClass='grid';
		$grd->columnHeaderClass='gridHeader';
		$grd->itemClass='gridItem';
		$grd->altItemClass='gridAltItem';
		$grd->fields='name, renderas, htmlclass, listname';
		$grd->columns= $_lang['kiwee_options_fld_name'] . ', ' . $_lang['kiwee_options_fld_renderas'] . ', '. $_lang['kiwee_options_fld_htmlclass'] . ', ' . $_lang['kiwee_options_fld_list'] . ',' . $_lang['kiwee_properties'] . ',' . $_lang['kiwee_delete'];
		$grd->colWidths='100,100,100,100,50,50';
		$grd->colAligns='left,left,left,left,center,center';
		$grd->colTypes=',,,,template:<button onClick="options_properties([+id+])">' . $_lang['kiwee_options_properties'] . '</button>,' .
					   'template:<button onClick="options_delete([+id+])">' . $_lang['kiwee_delete'] . '</button>';
		echo $grd->render();
	}

	// TODO: We should put this somewhere else, maybe standardize it in the class, or even move the renders to a database table
	// TODO: Lang entries
	function options_output_renders($sel = null) {
		//edited by Bright @ 20080806 added selectand edit parts
		$renders = array(array('DROP', 'Drop Down'), array('TEXT', 'Text Box'), array('TEXTAREA', 'Text Area'), array('CHECK', 'Check Boxes'), array('RADIO', 'Radio Buttons'), array('MULTI', 'Multi Select'), array('SELECTANDEDIT', 'Select and Edit'));
		$ret = array();
		foreach ($renders as $render) {
			$ret[] = '<option value="' . $render[0] . '" ';
			if ($sel == $render[0]) $ret[] = 'selected';
			$ret[] = '>' . $render[1] . '</option>';
		}
		return implode("\n", $ret);
	}

	function options_properties() {
		global $kiwee, $_lang;

		$id = intval($_POST['id']);
		
		$qry = 'select * from ' . $kiwee->config['fpx'] . 'options where id=' . $id;
		$rows = db_getrows($qry);
		if (sizeof($rows) < 1) die('ERROR: Could not query options table');

		$lines = array();
		//Edited by Bright @20080806
		//$lines[] = '<form method="POST" action="">';
		$lines[] = '<table border=0>';
		$lines[] = '<tr id="uoptset_name"><td><label style="color: white">' . $_lang['kiwee_options_fld_name'] . ':</label></td><td width="20">&nbsp;</td>';
		$lines[] = '<td><input id="uoptname" type="text" length=20 value="' . $rows[0]['name']. '"/></td></tr>';
		$lines[] = '<tr id="uoptset_render"><td><label style="color: white">' . $_lang['kiwee_options_fld_renderas'] . ':</label></td><td width="20">&nbsp;</td>';
		$lines[] = '<td><select onchange="uoptions_set_render();" id="uoptrender">';
		$lines[] = options_output_renders($rows[0]['renderas']);
		$lines[] = '</select><br />';

		$lines[] = '<tr id="uoptset_list" ';
		if (!in_array($rows[0]['renderas'], array('DROP', 'MULTI', 'CHECK', 'RADIO'))) $lines[] = 'style="display:none"';
		$lines[] = '><td><label style="color: white">' . $_lang['kiwee_options_fld_list'] . ':</label></td><td width="20">&nbsp;</td>	<td><select id="uoptlist" onchange="uoptions_set_list();">';
		$opts = $kiwee->getAllLists();
		foreach ($opts as $opt) {
			$lines[] = '<option value="' . $opt['id'] . '" ';
			if ($rows[0]['lists_id'] == $opt['id']) $lines[] = 'selected';
			$lines[] = '>' . $opt['description'] . '</option>';
		}
		$lines[] = '</select></td></tr>';
		
		$lines[] = '<tr id="uoptset_default" ';
		if (!in_array($rows[0]['renderas'], array('DROP', 'MULTI', 'CHECK', 'RADIO'))) $lines[] = 'style="display:none"';
		$lines[] = '><td><label style="color: white">' . $_lang['kiwee_options_fld_default'] . ':</label></td><td width="20">&nbsp;</td><td><select id="uoptdefault"/>';
		$opts = $kiwee->getListOptions($rows[0]['lists_id']);
		foreach ($opts as $opt) {
			$lines[] = '<option value="' . $opt['id'] . '" ';
			if ($rows[0]['deflt'] == $opt['id']) $lines[] = 'selected';
			$lines[] = '>' . $opt['name'] . '</option>';
		}
		$lines[] = '</select></td></tr>';
		
		$lines[] = '<tr id="uoptset_size" ';
		if (!in_array($rows[0]['renderas'], array('TEXT', 'MULTI'))) $lines[] = 'style="display:none"';
		$lines[] = '><td><label style="color: white">' . $_lang['kiwee_options_fld_size'] . ':</label></td><td width="20">&nbsp;</td>';
		$lines[] = '<td><input id="uoptsize" type="text" length=10 value="' . $rows[0]['size'] . '"/></td></tr>';
		
		$lines[] = '<tr id="uoptset_maxlen" ';
		if (!in_array($rows[0]['renderas'], array('TEXT'))) $lines[] = 'style="display:none"';
		$lines[] = '><td><label style="color: white">' . $_lang['kiwee_options_fld_maxlen'] . ':</label></td><td width="20">&nbsp;</td>';
		$lines[] = '<td><input id="uoptmaxlen" type="text" length=10 value="' . $rows[0]['maxlen'] . '"/></td></tr>';
		
		$lines[] = '<tr id="uoptset_rows" ';
		if (!in_array($rows[0]['renderas'], array('TEXTAREA'))) $lines[] = 'style="display:none"';
		$lines[] = '><td><label style="color: white">' . $_lang['kiwee_options_fld_rows'] . ':</label></td><td width="20">&nbsp;</td>';
		$lines[] = '<td><input id="uoptrows" type="text" length=10 value="' . $rows[0]['rows'] . '"/></td></tr>';

		$lines[] = '<tr id="uoptset_cols" ';
		if (!in_array($rows[0]['renderas'], array('TEXTAREA'))) $lines[] = 'style="display:none"';
		$lines[] = '><td><label style="color: white">' . $_lang['kiwee_options_fld_cols'] . ':</label></td><td width="20">&nbsp;</td>';
		$lines[] = '<td><input id="uoptcols" type="text" length=10 value="' . $rows[0]['cols'] . '"/></td></tr>';
		
		$lines[] = '<tr id="uoptset_class"><td><label style="color: white">' . $_lang['kiwee_options_fld_htmlclass'] . ':</label></td><td width="20">&nbsp;</td>';
		$lines[] = '<td><input id="uoptclass" type="text" length=100 value="' . $rows[0]['htmlclass'] . '"/></td></tr>';
		$lines[] = '</table>';
		$lines[] = '<button onClick="uoptions_update(' . $id . ')">' . $_lang['kiwee_options_save'] . '</button>&nbsp;&nbsp;&nbsp;';
		$lines[] = '<button onClick="hideBox()">' . $_lang['kiwee_options_cancel'] . '</button>';
		$lines[] = '</form>';

		echo implode("\n", $lines);
	}

	function options_update() {
		global $kiwee;
		
		$id = intval($_POST['id']);
		$name = addslashes(trim($_POST['name']));
		$renderas = addslashes(trim($_POST['renderas']));
		$htmlclass = addslashes(trim($_POST['htmlclass']));
		$list = intval($_POST['list']);
		$size = intval($_POST['size']);
		$maxlen = intval($_POST['maxlen']);
		$rows = intval($_POST['rows']);
		$cols = intval($_POST['cols']);
		$list = intval($_POST['list']);
		$default = intval($_POST['default']);
		
		if (strlen($name) < 1 || strlen($renderas) < 1)
			die('ERROR: Please supply a name.');
		
		$qry = 'update ' . $kiwee->config['fpx'] . 'options set lists_id=' . $list . ', name="' . $name . '", renderas="' . $renderas .
		  		'", htmlclass="' . $htmlclass . '", size=' . $size . ', maxlen=' . $maxlen . ', rows=' . $rows . 
		  		', cols=' . $cols . ', deflt=' . $default . ' where id=' . $id;
		mysql_query($qry);
		options_gridload();
	}

	function options_entries() {
		global $kiwee, $_lang;
		
		$id = intval($_POST['id']);

		$lines = array();

		$sql = 'SELECT * FROM ' . $kiwee->config['fpx'] . 'options_options where options_id=' . $id . ' order by value';
		$ds = mysql_query($sql) or die('ERROR: ' . mysql_error());
		$grd = new DataGrid('', $ds, $number_of_results);
		$grd->noRecordMsg = $_lang['no_records_found'];
		$grd->cssClass='grid';
		$grd->columnHeaderClass='gridHeader';
		$grd->itemClass='gridItem';
		$grd->altItemClass='gridAltItem';
		$grd->fields='name, value, priceoffset';
		$grd->columns= $_lang['kiwee_options_fld_name'] . ', ' . $_lang['kiwee_options_fld_value'] . ',' . $_lang['kiwee_options_fld_offset'];
		$grd->colWidths='100, 100, 50, 30';
		$grd->colAligns='left,left,left, center';
		$grd->colTypes=',,,template:<button onClick="options_entries_delete([+id+])">X</button>';
		$lines[] = $grd->render();
		$lines[] = '<form><input type=text id="options_name" size=10>&nbsp;&nbsp;';
		$lines[] = '<input type=text id="options_value" size=10>&nbsp;&nbsp;';
		$lines[] = '<input type=text id="options_offset" size=6>&nbsp;&nbsp;';
		$lines[] = '<input type="button" style="color: green" onclick="options_entries_add(' . $id . ')" value="' . $_lang['kiwee_options_add'] . '" name="options_e_add"></form>';
		echo implode("\n", $lines);
	}

	function options_entries_add() {
		global $kiwee;

		$id = intval($_POST['id']);
		$name = addslashes(trim($_POST['name']));
		$value = addslashes(trim($_POST['value']));
		$offset = floatval($_POST['offset']);
		
		$qry = 'insert into ' . $kiwee->config['fpx'] . 'options_options (options_id, name, value, priceoffset) values (' .
		  		$id . ',"' . $name . '","' . $value . '",' . $offset . ')';
		mysql_query($qry);
		options_entries();
	}		

	function options_entries_delete() {
		global $kiwee;
		
		$id = intval($_POST['id']);
		mysql_query('delete from ' . $kiwee->config['fpx'] . 'options_options where id=' . $id);
		options_entries();
	}		

	$oktxns[] = 'options_add';
	$oktxns[] = 'options_gridload';
	$oktxns[] = 'options_listentries';
	$oktxns[] = 'options_delete';
	$oktxns[] = 'options_properties';
	$oktxns[] = 'options_update';
	$oktxns[] = 'options_entries';
	$oktxns[] = 'options_entries_add';
	$oktxns[] = 'options_entries_delete';
?>